package com.app.datamanagermodule

import android.content.Context
import androidx.room.Database
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import com.app.datamanagermodule.dao.UserDao
import com.app.datamanagermodule.entity.User
import androidx.room.Room
import androidx.sqlite.db.SupportSQLiteDatabase
import androidx.room.migration.Migration





private var mAppDataBase: AppDatabase? = null

fun init(context: Context) {

    mAppDataBase = Room.databaseBuilder(context, AppDatabase::class.java, "learn_new_room_dev.db")
            .allowMainThreadQueries()
            //.addMigrations(MIGRATION_1_2, MIGRATION_2_3)
            .build()

}

fun getUserDao() : UserDao = mAppDataBase!!.userDao()

/**
 * 数据库版本 1->2 user表格新增了age列
 */
private val MIGRATION_1_2: Migration = object : Migration(1, 2) {
    override fun migrate(database: SupportSQLiteDatabase) {
        database.execSQL("ALTER TABLE User ADD COLUMN age integer")
    }
}


//@Database(entities = {User.class, Book.class}, version = 3)
//@TypeConverters({Converters.class})

@Database(entities = [User::class], version = 1)
abstract class AppDatabase : RoomDatabase() {

    abstract fun userDao() : UserDao

}